package com.tool4j.util.velocity;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;
import java.util.Properties;

/**
 * velocity模板渲染工具类
 *
 * @author Deng.Weiping
 * @since 2023/7/25 12:03
 */
@Slf4j
public class VelocityParserPlus {

    /**
     * 模板校验
     *
     * @param template
     */
    public static void verify(String template) {
        //设置velocity资源加载器
        initVelocity();
        //渲染模板
        Velocity.getTemplate(template, "UTF-8");
    }

    public static String execute(VelocityConfig config) throws IOException {
        //设置velocity资源加载器
        initVelocity();
        //封装模板数据
        VelocityContext context = getVelocityContext(config);
        //渲染模板
        Template tpl = Velocity.getTemplate(config.getTemplate(), "UTF-8");
        StringWriter sw = new StringWriter();
        tpl.merge(context, sw);
        String result = sw.toString();
        sw.close();
        return result;
    }

    private static void initVelocity() {
        Properties prop = new Properties();
        prop.put("file.resource.loader.class", "com.tool4j.util.velocity.VelocityTemplateLoader");
        Velocity.init(prop);
    }

    /**
     * 加载VelocityContext
     *
     * @param config
     * @return
     */
    private static VelocityContext getVelocityContext(VelocityConfig config) {
        Map<String, Object> map = BeanUtil.beanToMap(config);
        return new VelocityContext(map);
    }

    /**
     * 驼峰转首字母大写
     *
     * @param camelCase
     * @return
     */
    public static String getUpperName(String camelCase) {
        if (StrUtil.isBlank(camelCase)) {
            return camelCase;
        }
        return String.format("%s%s", camelCase.substring(0, 1).toUpperCase(), camelCase.substring(1));
    }


    /**
     * 驼峰转首字母小写
     *
     * @param camelCase
     * @return
     */
    public static String getLowerName(String camelCase) {
        if (StrUtil.isBlank(camelCase)) {
            return camelCase;
        }
        return String.format("%s%s", camelCase.substring(0, 1).toLowerCase(), camelCase.substring(1));
    }
}
